<?php
namespace Users\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class RatesTable
{
    protected $tableGateway;
	protected $select;
	protected $resultSet;
	
	public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function getRatesdetails($rateid){
		$select = $this->tableGateway->getSql()->select();
			$select	->where("sno='".$rateid."'");
		$resultSet = $this->tableGateway->selectWith($select);
		return 	$resultSet->current();	
	}
	public function deleteRates($ids){		
		$this->tableGateway->delete(array('(sno IN ('.$ids.'))'));			
		return $this->tableGateway->lastInsertValue;	
	}
	public function deleteAllRates(){
		$this->tableGateway->delete('sno!=""');	
	}
	
	 public function addCountry($rates)
     {	
		$data=array(
	 			 'country'			=>	$rates['country'],
				 'country_id'		=>	$rates['country_id'],
			     'landline'			=>	$rates['landline'],
				 'mobile'			=>	$rates['mobile'],
				 'price'			=>	$rates['price'],
				 'image'			=>	$rates['image'],
				 'status'			=>	0,
			);				
		if(isset($rates['hidsno']) && $rates['hidsno']!=""){			
			$row=$this->tableGateway->update($data, array('sno' => $rates['hidsno']));	
		}else{			
			$row=$this->tableGateway->insert($data);
		}
		return $row;
     }
	 public function updateRates($rates)
     {
			$data=array(
	 			 'AreaPrefix'		=>	$rates['areaPrefix'],
				 'CountryName'		=>	$rates['country'],
			     'Preminum'			=>	$rates['preminum'],
				 'Standerd'			=>	$rates['standerd'],
				 'BillingCycle'		=>	$rates['billingCycle'],
			);
			
			$row=$this->tableGateway->update($data, array('sno' => $rates['id']));	
			return $row;
	}
	 
	 public function editrates($data)
     {	
		$select = $this->tableGateway->getSql()->select();
					$select->where('sno="'.$data.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();		
	}
	 public function fetchAll($data)
     {
		$select = $this->tableGateway->getSql()->select();
					$select->where(new \Zend\Db\Sql\Predicate\Like('CountryName', $data.'%'));
			$resultSet = $this->tableGateway->selectWith($select);
			return $resultSet;
     }
	 public function AllRates(){
		$select = $this->tableGateway->getSql()->select();				
		$resultSet = $this->tableGateway->selectWith($select);			
		return $resultSet;
	 }
	 public function getAllRates(){
		$select = $this->tableGateway->getSql()->select();				
		$resultSet = $this->tableGateway->selectWith($select);			
		return $resultSet->count();
	 }
	 public function getratesGrid($aColumns,$data,$paginated=false){
		$select = $this->tableGateway->getSql()->select();		
		
		// Search
		if(isset($data['sSearch'])){
			if($data['sSearch']!='0' && $data['sSearch']!='')
			{
				$select->where(array(
					new Predicate\PredicateSet(
						array(
						new Predicate\Like('CountryName', '%'.$data['sSearch'].'%'),
						),
							Predicate\PredicateSet::COMBINED_BY_OR
					),
				));
			}
		}
		// End
		// Pagination
		if(isset($data['iDisplayStart']) && $data['iDisplayLength'] != '-1')
		{
			$start=(int)$data['iDisplayStart'];
			$currentPage = (int)$data['iDisplayLength'];
			$resultsPerPage = (int) $currentPage;
			$select->order('sno DESC')->limit($currentPage)->offset($start);
		}
		// End
		
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
	 }
}


